<?php

class Omedia extends Controller
{
	/*
	 * constructor
	 */
	function __construct ()
	{
		parent::Controller();
		$this->load->model('Articles');
	}
	function admin ()
	{
		
	}
	function news ($section = NULL, $under_section = NULL)
	{
		$this->_base($section, $under_section);
	}
	function index ()
	{
		/*
		 * Trying out a way to divide the content into
		 * channels and blocks
		 */
		$this->_base();
	}
	function _process_block ($block)
	{
		//return '<pre>' . print_r($block, true) . '</pre>';
		$ret = '';
		
		$sources = array();
		foreach ($block['sources'] as $k => $s)
		{
			//echo '<b>' . $k . '</b>';
			$sources[$k] = $s;
		}
			
		if ($block['type'] == 'newsstream')
		{
			$article_block = $block['article_block'];
			foreach ($block['newsstream'] as $n)
			{
				//$ret = '<pre>' . print_r($n['article_content'], TRUE) . '</pre>';
				$ret .= $this->parser->parse('blocks/' . $article_block, $n['article_content'], TRUE);
			}
		}
		else if ($block['type'] == 'multisource')
		{
			$custom_block = $block['custom_block'];
			
			$ret .= $this->parser->parse('blocks/' . $custom_block, $sources, TRUE);
		}
		else
		{
			$custom_block = $block['custom_block'];
			$ret .= $this->parser->parse('blocks/' . $custom_block, $sources, TRUE);
		}
		//return print_r($block, TRUE);
		return $ret;
	}
	function _base ($active_section = NULL, $active_under_section = NULL)
	{
		$this->load->model('Articles');
		$menu = $this->Articles->get_menu();
		
		$tree = $this->Articles->get_tree();
		
		
		$channels['right'] = array(
			'blocks' 	=> array(
				array(
					'block' => 'ten_most_read',
					'innersql' => 'SELECT header FROM om_articles LIMIT 0,10'
				)
			),
		);
		$channels['main'] = array(
			'type' => 'custom',
			'news' => array(
				array('block' => 'front_articles', 'order' => 0, 'type' => 'article', 'id' => 1)
			)
		);
		
		/*
		 * Check menu 
		 */
		
		foreach ($menu as $key => $value)
		{
			if (isset($active_section))
			{
				if ($key == $active_section)
				{
					$menu[$key]['selected'] = true;
				}
			}
		}
		
		/*
		 * 
		 */
		
		//exit;
		$data = array();
		$data['menu'] = $menu;
		foreach ($tree as $key => $channel)
		{
			$data[$key] = '';
			foreach ($channel['blocks'] as $block)
			{
				$data[$key] .= $this->_process_block($block);
			}
		}
		
		/*
		foreach ($channels as $key => $content)
		{
			// check if channel variable is set
			if (!isset($data[$key])) $data[$key] = '';

			foreach ($content['news'] as $blockcontent)
			{
				$block = $blockcontent['block'];
				$blockdata = array();
				// 
				if (isset($blockcontent['sql']))
				{
					$sql = $blockcontent['sql'];
					$q = $this->Articles->get_query($sql);
					foreach ($q as $row)
					{
						$blockdata[] = $row;
					}
					
				}
				else if (isset($blockcontent['innersql']))
				{
					$sql = $blockcontent['innersql'];
					$q = $this->Articles->get_query($sql);
					foreach ($q as $row)
					{
						$blockdata[] = $row;
					}
					
				}
				
				// add block data to channel
				if (@file_exists(APPPATH."views/blocks/{$block}.php"))
				{
					if (!is_array($blockdata))
					{
						$data[$key] .= $this->parser->parse('blocks/' . $block, $blockdata, true);
					}
					else
					{
						if (isset($blockcontent['sql']))
						{
							foreach ($blockdata as $b)
							{
								$data[$key] .= $this->parser->parse('blocks/' . $block, $b, true);
							}
						}
						else if (isset($blockcontent['innersql']))
						{
							$data[$key] .= $this->parser->parse('blocks/' . $block, array('query' => $blockdata), true);
						}
					}
				}
				else
				{
					$data[$key] .= '<br/>Block doesn\'t exist.';
				}
			}
		}
		*/
		$this->load->view('layout/header', $data);
		$this->load->view('layout/footer');
		//echo '<pre>'; print_r($tree); echo '</pre>';
	}
}
?>